/**
 * Created by yangfan on 2016/9/21.
 */
/**
 * Created by yangfan on 2016/8/31.
 */
define(['config/appregister',"../../services/httpServices","i18n","config/editor","webuploader"],function(app,httpService,i18n,editor,webuploader){
    app.directive("uploadSelect",function(){
        return {
            scope:{conf:"=uploadConf"},
            template:'<input type="file" id="file" ng-click="selectFile()"   style="width:238px;height:60px;opacity:0;cursor: pointer;"  />',
            link:function(scope,element,attrs){
                var file = document.getElementById("file");
                if(!scope.conf.url||!scope.conf.api){
                    return
                }
                var url = scope.conf.url + "/" + scope.conf.api;

                file.onchange = function () {
                        var ajaxFile=new uploadFile({
                            //"url":"http://115.29.168.57:8080/elink_database/api/file/upload",
                            url:url,
                            "dataType":"jsonp",
                            "timeout":5000,
                            "async":true,
                            "data":{
                                //多文件
//					"files":{
//						//file为name字段 后台可以通过$_FILES["file"]获得
//						"file":document.getElementById("file").files//文件数组
//					}
                                //单文件
                                "file":{
                                    "file":document.getElementById("file").files[0],
                                },
                            },
                            onloadstart:function(){
                                //console.log("开始上传");
                            },
                            onload:function(data){
                                console.log(data);
                                console.log(data.name);
                            },
                            onerror:function(er){
                                console.log("上传失败XXX");
                            },
                            onabort:function(){
                                //alert("取消上传");
                                console.log("cancel");
                            },
                            ontimeout:function(){
                                //alert("上传时间到");
                            },
                            onloadend:function(data){
                                if(!data.response){
                                    return
                                };
                               var json =JSON.parse(data.response);
                                scope.conf.path = json.data;
                            },
                            onprogress:function(e){
                                console.log(e);
                            }
                        });
                    scope.conf.onchange();
                }
            }
        }
    });

});
